package com.imy.lua.view;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.imy.common.base.BaseCustomViewGroup;
import com.imy.common.util.ToastUtil;
import com.imy.common.util.UiUtil;
import com.imy.lua.R;

/**
 * create by Imy
 */
public class ColorImageToolsView extends BaseCustomViewGroup {
    private LinearLayout imageLl;
    private TextView imageTestTv;
    private TextView imageSaveTv;
    private TextView imageGiveUpTv;
    private LinearLayout colorGetLl;
    private TextView colorExitTv;
    private TextView colorCodeTv;
    private LinearLayout colorFindLL;
    private TextView colorFindExitTv;
    private TextView colorFindInRegionCodeTv;
    private TextView colorFindInRegionTestTv;
    public ColorImageToolsView(Context context) {
        super(context);
    }

    public ColorImageToolsView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ColorImageToolsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void setOrientation(int orientation) {
        this.ori = orientation;
    }
    private int ori = Configuration.ORIENTATION_PORTRAIT;
    private float downX;
    private float downY;
    @Override
    protected void init(View inflate) {
        colorGetLl=inflate.findViewById(R.id.color_get_ll);
        colorCodeTv =inflate.findViewById(R.id.color_code_tv);
        colorExitTv =inflate.findViewById(R.id.color_give_up_tv);
        imageLl =inflate.findViewById(R.id.image_ocr_ll);
        imageSaveTv =inflate.findViewById(R.id.image_save_tv);
        imageTestTv= inflate.findViewById(R.id.image_test_tv);
        imageGiveUpTv =inflate.findViewById(R.id.image_give_up_tv);
        initColorFind(inflate);
        imageTestTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (listener!=null){
                    listener.onImageTest();
                }
            }
        });
        imageSaveTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onImageSave();
                }
            }
        });
        imageGiveUpTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onImageGiveUp();
                }
            }
        });
        colorExitTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                colorGetLl.setVisibility(GONE);
                if (listener!=null){
                    listener.onColorExit();
                }
            }
        });
        colorCodeTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onColorCodeMake();
                }
            }
        });
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        downX = event.getRawX();
                        downY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float rawX = event.getRawX();
                        float rawY = event.getRawY();
                        if (Math.abs(downX - rawX) < 10 && Math.abs(downY - rawY) < 10) {
                            //过滤掉抖动
                            break;
                        }
                        //直接更新xy
                        int x;
                        int width = getMeasuredWidth() / 2;
                        if (ori == Configuration.ORIENTATION_LANDSCAPE) {
                            x = (int) event.getRawX() - getMeasuredWidth();
                        } else {
                            x = (int) event.getRawX() - width;
                        }
                        if (x < 0) {
                            x = 0;
                        }
                        int y;
                        if (ori == Configuration.ORIENTATION_LANDSCAPE) {
                            int i = getMeasuredHeight() / 2;
                            y = (int) event.getRawY() - i;
                        } else {
                            y = (int) event.getRawY() - getMeasuredHeight();
                        }
                        if (y < 0) {
                            y = 0;
                        }
                        setX(x);
                        setY(y);
                        break;
                    case MotionEvent.ACTION_UP:
                        rawX = event.getRawX();
                        rawY = event.getRawY();
                        if (Math.abs(downX - rawX) < 10 && Math.abs(downY - rawY) < 10) {
                            onClicked();
                        } else {
                            //判断是否需要依附到边上
                            checkMove(rawX, rawY);
                        }
                        break;
                }
                return true;
            }
        });
        setY(UiUtil.getDeviceHeight(mContext)/3);
        measure(0,0);
        setX(UiUtil.getDeviceWidth(mContext)-getMeasuredWidth());
    }

    private void initColorFind(View inflate) {
        colorFindLL =inflate.findViewById(R.id.color_find_in_region_ll);
        colorFindExitTv =inflate.findViewById(R.id.color_find_in_region_give_up_tv);
        colorFindInRegionCodeTv =inflate.findViewById(R.id.color_find_in_region_code_tv);
        colorFindInRegionTestTv =inflate.findViewById(R.id.color_find_in_region_test_tv);
        colorFindInRegionCodeTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onColorFindCodeCreate();
            }
        });
        colorFindExitTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                colorFindLL.setVisibility(GONE);
                listener.onColorFindExit();
            }
        });
        colorFindInRegionTestTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onColorFindTest();
            }
        });
    }

    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    private int mCurrentStatus;
    private void checkMove(float rawX, float rawY) {
        //判断是否需要依附到边上
        int deviceWidth = UiUtil.getDeviceWidth(getContext());
        int width_half = deviceWidth / 2;

        if (rawX < width_half) {//靠左边
            mCurrentStatus = LEFT;
            startXAnim((int) rawX, 0);
        } else {
            mCurrentStatus = RIGHT;
            startXAnim((int) rawX, deviceWidth - getMeasuredWidth());
        }
    }
    private boolean inAnim = false;
    private void startXAnim(int start, int end) {
        inAnim = true;
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Context context = getContext();
                if (context instanceof Activity) {
                    Activity activity = (Activity) context;
                    if (activity.isFinishing()) {
                        return;
                    }
                }
                int animatedValue = (int) animation.getAnimatedValue();
                setX(animatedValue);
                if (animation.getAnimatedFraction() == 1) {
                    inAnim = false;

                }
            }
        });
        valueAnimator.start();
    }

    private void onClicked() {
        if (listener!=null){
            listener.onClicked();
        }
    }

    public void  showImageTools(){
        imageLl.setVisibility(VISIBLE);
    }
    public void hideImageTools(){
        imageLl.setVisibility(GONE);
    }
    @Override
    protected int getLayoutID() {
        return R.layout.custom_view_color_image_tools;
    }

    public void showColorGet() {
            colorGetLl.setVisibility(VISIBLE);
    }

    public void showColorFindInRegion() {
        colorFindLL.setVisibility(VISIBLE);
    }

    public interface FloatingListener {
        void onClicked();
        void onImageGiveUp();
        void onImageTest();
        void onImageSave();

        void onColorExit();

        void onColorCodeMake();

        void onColorFindExit();

        void onColorFindTest();

        void onColorFindCodeCreate();
    }

    public void setFloatingListener(FloatingListener listener) {
        this.listener = listener;
    }

    private FloatingListener listener;
}
